package com.qf.serivce.impl;

import com.qf.dao.TeacherRepository;
import com.qf.pojo.entity.QfTeach;
import com.qf.pojo.repVo.BaseResp;
import com.qf.serivce.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    TeacherRepository teacherRepository;

    @Autowired
    RedisTemplate redisTemplate;

    private static  final String TEACHER_KEY="teacher-list";

    @Override
    public BaseResp findAll() {

        Boolean aBoolean = redisTemplate.hasKey(TEACHER_KEY);
        List range=null;
        if (aBoolean){
             range = redisTemplate.opsForList().range(TEACHER_KEY, 0, -1);
        }else{
            range = teacherRepository.findAll();
            redisTemplate.opsForList().rightPushAll(TEACHER_KEY,range);
        }
        return new BaseResp(1,"查询成功！",range,null);
    }

    @Override
    public BaseResp findById(Integer id) {

        Optional<QfTeach> byId = teacherRepository.findById(id);
        if (!byId.isPresent()){
            return new BaseResp(0,"查询失败！",null,null);
        }
        return new BaseResp(1,"查询成功！",byId.get(),null);
    }
}
